package com.resou.reader.bookstore;

import android.util.Log;

import com.resou.reader.api.entry.CarousePicBean;
import com.resou.reader.api.entry.CommonData;
import com.resou.reader.api.entry.ResultList;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.Injection;
import com.resou.reader.data.bookstore.BookStoreRepository;
import com.resou.reader.data.bookstore.model.BasicList;
import com.resou.reader.data.bookstore.model.PlateNovels;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.BiFunction;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class BookStorePresenter extends ResouBasePresenter<IBookStoreView> {
    private boolean firstLoad = true;
    private static final String TAG = "BookStorePresenter";
    private final BookStoreRepository mRepository;

    public BookStorePresenter(IBookStoreView view) {
        super(view);
        mRepository = Injection.provideBookstoreRepository();
    }

    public void loadBookStoreInfo(String gender) {
        if (firstLoad) {
            mView.showProgress();
        }
        addCompositeDisposable(
                Observable.zip(mRepository.getBannerPic(gender), mRepository.getMainRankList(gender), new BiFunction<CommonData<List<CarousePicBean>>, List<BasicList<PlateNovels>>, List<Object>>() {
                    @Override
                    public List<Object> apply(CommonData<List<CarousePicBean>> listCommonData, List<BasicList<PlateNovels>> basicLists) throws Exception {
                        ArrayList<Object> objects = new ArrayList<>();
                        objects.add(listCommonData);
                        objects.add(null);
                        objects.addAll(basicLists);
                        return objects;
                    }
                })
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(objects -> {
                            mView.setData(objects);
                            firstLoad = false;
                            mView.showContent();
                            mView.finishRefresh();
                        }, throwable -> {
                            mView.showError(throwable);
                            mView.finishRefresh();
                        })
        );

    }

    public void getRepeatList() {
        mRepository.getRepeatList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<ResultList<PlateNovels>>() {

                    @Override
                    public void onNext(ResultList<PlateNovels> plateNovelsResultList) {
                        if (plateNovelsResultList != null) {
                            mView.getRepeatList(plateNovelsResultList);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError:网络 添加错误,错误信息:" + e.getMessage());
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
